package top.hcode.hoj.dao.org;

import cn.hutool.core.bean.DynaBean;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.springframework.stereotype.Service;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.mapper.OrgUserMapper;
import top.hcode.hoj.pojo.entity.org.OrgUser;
import top.hcode.hoj.utils.SqlUtil;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author orangej
 * @since 2025-01-21
 */
@Service
@RequiredArgsConstructor
public class OrgUserDao extends ServiceImpl<OrgUserMapper, OrgUser> {
    private final LazyDao dao;

    public OrgUser getByUsernameOrMobile(String username, String mobile) {
        if (Strings.isNotBlank(username)) {
            return lambdaQuery().eq(OrgUser::getUsername, username).one();
        } else if (Strings.isNotBlank(mobile)) {
            return lambdaQuery().eq(OrgUser::getMobile, mobile).one();
        }

        return null;
    }

    public void loadOrgUserLink(List<?> objList) {
        if (Lang.isEmpty(objList)) return;

        List<DynaBean> beanList = objList.stream().map(DynaBean::new).collect(Collectors.toList());
        List<String> userIdList = beanList.stream().map(o -> (String) o.get("userId")).collect(Collectors.toList());
        List<OrgUser> orgUserList = lambdaQuery()
                .select(OrgUser::getId, OrgUser::getUsername, OrgUser::getRealName, OrgUser::getAvatar)
                .in(OrgUser::getId, userIdList).list();
        Map<String, OrgUser> orgUserMap = orgUserList.stream().collect(Collectors.toMap(OrgUser::getId, orgUser -> orgUser));

        for (DynaBean bean : beanList) {
            bean.set("user", orgUserMap.get((String) bean.get("userId")));
        }
    }

    public String getRealNameByUid(String uid) {
        OrgUser user = lambdaQuery().eq(OrgUser::getId, uid).select(OrgUser::getRealName).one();
        if (user == null) {
            return null;
        }
        return user.getRealName();
    }

    public String getStudentClassId(String userId) {
        return dao.selectOne(String.class,
                "select org_id from class_has_user where user_id = ? and user_type = 0 limit 1", userId);
    }

    public String getStudentSchoolId(String userId) {
        return dao.selectOne(String.class,
                "select org_id from school_has_user where user_id = ? and user_type = 0 limit 1", userId);
    }

    public Integer getStudentGrade(String userId) {
        return dao.selectOne(Integer.class,
                "select grade_num from org where id = " +
                        "(select org_id from class_has_user where user_id = ? and user_type = 0 limit 1)", userId);
    }

    public String getStudentClassName(String userId) {
        return dao.selectOne(String.class,
                SqlUtil.sql("select org_name from org where id = " +
                        "(select org_id from class_has_user where user_id = ? and user_type = 0 limit 1)"), userId);
    }
}
